package com.mateng.treasurechest.presenter.weather

import android.util.Log
import com.mateng.treasurechest.model.weather.bean.EveryDayImgBean
import com.mateng.treasurechest.model.weather.service.IWeatherService
import com.mateng.treasurechest.presenter.BasePresenter
import com.mateng.treasurechest.utils.GsonUtils
import com.mateng.treasurechest.view.weather.callback.IWeatherResultCallBack
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

/**
 * Created by 樊磊 on 2018/2/2.
 */

class WeatherPresenter : BasePresenter() {

    // 获取每日一图
    fun http_getEveryDayImg(mListener: IWeatherResultCallBack) {
        registerSubscription(IWeatherService.instance.getEveryDayImg("http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(object : Subscriber<EveryDayImgBean>() {
                    override fun onNext(t: EveryDayImgBean?) {
                        mListener.let { mListener.onEveryDayImg(t) }
                        t?.let { Log.d("WeatherPresenter", GsonUtils.instance.objectToJson(it)) }
                    }

                    override fun onCompleted() {

                    }

                    override fun onError(e: Throwable?) {

                    }

                }))
    }

}

